package com.gzz.orchid.main.console.controller.rbac;

import com.gzz.orchid.common.core.model.ActionCmd;
import com.gzz.orchid.common.core.tuple.Option;
import com.gzz.orchid.common.webmvc.support.HttpResult;
import com.gzz.orchid.context.application.rbac.IRoleExecutor;
import com.gzz.orchid.context.application.rbac.IRoleHandler;
import com.gzz.orchid.context.application.rbac.command.RoleSaveCmd;
import com.gzz.orchid.context.application.rbac.pojo.RoleDto;
import com.gzz.orchid.context.application.rbac.pojo.RoleVo;
import com.gzz.orchid.context.application.rbac.queries.RoleQuery;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author higmd
 */
@Slf4j
@RestController
@RequestMapping("/rbac/role")
@RequiredArgsConstructor
public class RoleController {
    private final IRoleExecutor executor;
    private final IRoleHandler handler;

    /**
     * 获取选项
     *
     * @return
     */
    @RequestMapping("/options")
    public HttpResult options() {
        log.info("获取选项");
        List<Option<String>> optionList = handler.options();
        return HttpResult.success()
                .data(optionList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public HttpResult list(RoleQuery query) {
        log.info("列表:{}", query);
        List<RoleDto> pageData = handler.listByPager(query);
        return HttpResult.success()
                .data(pageData)
                .pager(query.getPager());
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    public HttpResult detail(@NotNull(message = "required.parameter") Long id) {
        log.info("详情: {}", id);
        RoleVo detail = handler.detail(id);
        return HttpResult.success()
                .data(detail);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public HttpResult save(@RequestBody @Valid RoleSaveCmd saveCmd) {
        log.info("保存:{}", saveCmd);
        executor.save(saveCmd);
        return HttpResult.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public HttpResult delete(@Valid ActionCmd cmd) {
        log.info("删除:{}", cmd);
        executor.delete(cmd);
        return HttpResult.success();
    }

    /**
     * 禁用
     */
    @RequestMapping("/disable")
    public HttpResult disable(@Valid ActionCmd cmd) {
        log.info("禁用:{}", cmd);
        executor.disable(cmd);
        return HttpResult.success();
    }

    /**
     * 启用
     */
    @RequestMapping("/enable")
    public HttpResult enable(@Valid ActionCmd cmd) {
        log.info("启用:{}", cmd);
        executor.enable(cmd);
        return HttpResult.success();
    }

    /**
     * 审核
     */
    @RequestMapping("/audit")
    public HttpResult audit(@Valid ActionCmd cmd) {
        log.info("审核:{}", cmd);
        executor.audit(cmd);
        return HttpResult.success();
    }
}
